package com.stenio.fancat.controller.behavior;

import com.stenio.fancat.controller.Result;
import com.stenio.fancat.domain.bo.BehaviorLogBO;
import com.stenio.fancat.exception.FanCatException;
import com.stenio.fancat.form.behavior.BehaviorCommentsForm;
import com.stenio.fancat.form.behavior.BehaviorForm;
import com.stenio.fancat.form.behavior.BehaviorLogForm;
import com.stenio.fancat.form.user.UserRelationshipForm;
import com.stenio.fancat.service.behavior.BehaviorLogService;
import com.stenio.fancat.vo.BehaviorCommentsVO;
import com.stenio.fancat.vo.BehaviorLogVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/behaviorLog")
public class BehaviorLogController {

    @Autowired
    private BehaviorLogService behaviorLogService;

    /**
     * 查询亲人圈列表
     * @param userRelationshipForm
     * @param behaviorForm
     * @return
     */
    @PostMapping(value = "/selectOwnBehaviorList")
    public Result<Map<String,Object>> selectOwnBehaviorList(@RequestBody UserRelationshipForm userRelationshipForm,
                                                            @RequestBody BehaviorForm behaviorForm){
        Result<Map<String,Object>> result = new Result<>();
        Map<String,Object> map = null;
        try {
            map = behaviorLogService.selectOwnBehaviorList(userRelationshipForm,behaviorForm);
        } catch (FanCatException e) {
            result.setMsg(e.getMsg());
            result.setErrorCode(e.getErrorCode());
            result.setFlag(Result.FLAG.failure);
            return result;
        }
        result.setData(map);
        result.setFlag(Result.FLAG.success);
        return result;
    }

    /**
     * 查询亲人圈列表  没有分类的
     * @param userRelationshipForm
     * @return
     */
    @PostMapping(value = "/selectOwnBehaviorListNoClassify")
    public Result<Map<String,Object>> selectOwnBehaviorListNoClassify(@RequestBody UserRelationshipForm userRelationshipForm){
        Result<Map<String,Object>> result = new Result<>();
        Map<String,Object> map = null;
        try {
            map = behaviorLogService.selectOwnBehaviorListNoClassify(userRelationshipForm);
        } catch (FanCatException e) {
            result.setMsg(e.getMsg());
            result.setErrorCode(e.getErrorCode());
            result.setFlag(Result.FLAG.failure);
            return result;
        }
        result.setData(map);
        result.setFlag(Result.FLAG.success);
        return result;
    }



    /**
     * 删除个人任务
     * @param userRelationshipForm
     * @param behaviorLogForm
     * @return
     */
    @PostMapping("/deleteBehavior")
    public Result<Integer> deleteBehavior (@RequestBody UserRelationshipForm userRelationshipForm,
                                           @RequestBody BehaviorLogForm behaviorLogForm){
        Integer num = null;
        Result<Integer> result = new Result<>();
        try {
            num = behaviorLogService.deleteBehavior(userRelationshipForm,behaviorLogForm);
        } catch (FanCatException e) {
            result.setMsg(e.getMsg());
            result.setErrorCode(e.getErrorCode());
            result.setFlag(Result.FLAG.failure);
            return result;
        }

        if(num > 0){
            result.setData(num);
            result.setFlag(Result.FLAG.success);
        }
        return result;
    }

    /**
     * 增加评论
     * @param behaviorCommentsForm
     * @return
     */
    @PostMapping("insertComments")
    public Result<BehaviorCommentsVO> insertComments(@RequestBody(required = false) BehaviorCommentsForm behaviorCommentsForm){
        Result<BehaviorCommentsVO> result = new Result<>();
        BehaviorCommentsVO behaviorComments = null;
        try {
            behaviorComments = behaviorLogService.insertComments(behaviorCommentsForm);
        } catch (FanCatException e) {
            result.setMsg(e.getMsg());
            result.setErrorCode(e.getErrorCode());
            result.setFlag(Result.FLAG.failure);
            return result;
        }

        result.setData(behaviorComments);
        result.setFlag(Result.FLAG.success);

        return result;
    }

    /**
     * 删除评论   （根据评论id删除）
     * @param behaviorCommentsForm
     * @return
     */
    @PostMapping(value = "deleteComments",produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<Integer> deleteComments(@RequestBody(required = false) BehaviorCommentsForm behaviorCommentsForm){
        Result<Integer> result = new Result<>();
        Integer num = null;
        try {
            num = behaviorLogService.deleteComments(behaviorCommentsForm);
        } catch (FanCatException e) {
            result.setMsg(e.getMsg());
            result.setErrorCode(e.getErrorCode());
            result.setFlag(Result.FLAG.failure);
            return result;
        }

        result.setData(num);
        result.setFlag(Result.FLAG.success);

        return result;
    }

    /**
     * 上传个人任务（上传图片）
     * @param file
     * @return
     */
    @PostMapping(value = "insertOwnBehaviorImages",produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<String> insertOwnBehaviorImages(MultipartFile file, BehaviorLogForm behaviorLogForm){
        Result<String> result = new Result<>();
        String url = null;
        try {
            url = behaviorLogService.insertOwnBehaviorImages(file,behaviorLogForm);
        } catch (FanCatException e) {
            result.setMsg(e.getMsg());
            result.setErrorCode(e.getErrorCode());
            result.setFlag(Result.FLAG.failure);
            return result;
        }

        result.setData(url);
        result.setFlag(Result.FLAG.success);
        return result;
    }

    /**
     * 上传个人任务
     * @param behaviorLogForm
     * @return
     */
    @PostMapping("insertOwnBehavior")
    public Result<BehaviorLogVO> insertOwnBehavior (@RequestBody BehaviorLogForm behaviorLogForm){
        Result<BehaviorLogVO> result = new Result<>();
        BehaviorLogVO behaviorLogVO = null;
        try {
            behaviorLogVO = behaviorLogService.insertOwnBehavior(behaviorLogForm);
        } catch (FanCatException e) {
            result.setMsg(e.getMsg());
            result.setErrorCode(e.getErrorCode());
            result.setFlag(Result.FLAG.failure);
            return result;
        }

        result.setData(behaviorLogVO);
        result.setFlag(Result.FLAG.success);

        return result;
    }

    /**
     *  查询伙伴圈
     * @param userRelationshipForm
     * @return
     */
    @PostMapping("selectBehaviorList")
    public Result<Map<String,Object>> selectBehaviorList(@RequestBody UserRelationshipForm userRelationshipForm){
        Result<Map<String,Object>> result = new Result<>();
        try {
            Map<String,Object> map =  behaviorLogService.selectBehaviorList(userRelationshipForm);
            result.setData(map);
            result.setFlag(Result.FLAG.success);
        } catch (FanCatException e) {
            result.setMsg(e.getMsg());
            result.setErrorCode(e.getErrorCode());
            result.setFlag(Result.FLAG.failure);
            return result;
        }

        return result;
    }

    /**
     * 伙伴动态详情
     * @param userRelationshipForm
     * @return
     */
    @PostMapping("selectPartnerBehaviorList")
    public Result<List<BehaviorLogBO>> selectPartnerBehaviorList(@RequestBody UserRelationshipForm userRelationshipForm){
        Result<List<BehaviorLogBO>> result = new Result<>();
        List<BehaviorLogBO> list = null;
        try {
            list = behaviorLogService.selectPartnerBehaviorList(userRelationshipForm);
        } catch (FanCatException e) {
            result.setMsg(e.getMsg());
            result.setErrorCode(e.getErrorCode());
            result.setFlag(Result.FLAG.failure);
            return result;
        }
        result.setData(list);
        result.setFlag(Result.FLAG.success);
        return result;
    }

}
